/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.chinalwb.are.view;

import com.chinalwb.are.ResourceTable;
import com.chinalwb.are.utils.FindUtils;
import com.chinalwb.are.utils.PxUtil;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;

/**
 * 字体大小
 *
 * @author zh
 * @since 2021/06/22
 */
public class SelectFontSizeDialog extends PopupDialog {
    private Component dialogView;
    private final Context context;
    private int size;

    public SelectFontSizeDialog(Context context, Component contentComponent, int textSize) {
        super(context, contentComponent);
        this.context = context;
        this.size = textSize;
        initView();
    }

    private void initView() {
        dialogView = LayoutScatter.getInstance(context).parse(
                ResourceTable.Layout_dialog_select_font_size,
                null, false);
        setAlignment(LayoutAlignment.TOP);
        setBackColor(Color.TRANSPARENT);
        setTransparent(true);
        setCustomComponent(dialogView);
        Slider slider = FindUtils.findSliderById(dialogView, ResourceTable.Id_wave_slider);
        Text proTv = FindUtils.findTextById(dialogView, ResourceTable.Id_text);
        slider.setProgressValue(size);
        proTv.setTextSize(AttrHelper.fp2px(size, context));
        proTv.setText(size + "fp:priview");
        slider.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean b) {
                proTv.setTextSize(AttrHelper.fp2px(progress, context));
                proTv.setText(progress + "fp:priview");
                size = progress;
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
        setDialogListener(() -> {
            setRemoveDialog();
            return false;
        });
    }

    /**
     * 销毁dialog
     */
    private void setRemoveDialog() {
        dialogView = null;
        remove();
        destroy();
        if (size > 0) {
            selectTextSizeListener.onSelectTextSizeListener(size);
        }
    }

    private SelectTextSizeListener selectTextSizeListener;

    /**
     * selectTextSizeListener
     *
     * @param selectTextSizeListener selectTextSizeListener
     */
    public void setSelectColorListener(SelectTextSizeListener selectTextSizeListener) {
        this.selectTextSizeListener = selectTextSizeListener;
    }

    /**
     * SelectTextSizeListener
     */
    public interface SelectTextSizeListener {
        void onSelectTextSizeListener(int size);
    }
}
